using Microsoft.VisualBasic.Devices;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace 文件名批量修改
{
    public partial class 文件名批量修改Form : Form
    {
        FileInfo[] fi;
        int index1;



        public 文件名批量修改Form()
        {
            InitializeComponent();
        }
        private void 文件名批量修改Form_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("序号");
            dt.Columns.Add("旧名称");
            dt.Columns.Add("新名称");
            dataGridView1.DataSource = dt;
            timer1.Enabled = true;
        }

        private void 选择文件夹_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "选择文件夹"; ;//左上角提示
            string path = string.Empty;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                path = dialog.SelectedPath;//获取选中文件路径
            }
            label1.Text = path;
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] fi = di.GetFiles();


            DataTable dt = new DataTable();
            dt.Columns.Add("序号");
            dt.Columns.Add("旧名称");
            dt.Columns.Add("新名称");

            index1 = fi.Length;
            for (int i = 0; i < fi.Length; i++)
            {
                dt.Rows.Add(i + 1, fi[i].Name);
            }


            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[1].ReadOnly = true;
            // 让 DataGridView1 的所有列宽自动调整一下。

            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            if (dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit))//判断datagridview1内容是否有修改
            {
                // 让 DataGridView1 的所有列宽自动调整一下。
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            }
        }

        private void 去除指定文字_Click(object sender, EventArgs e)
        {

            try
            {
                for (int i = 0; i < index1; i++)
                {
                    string oldstr = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    dataGridView1.Rows[i].Cells[2].Value = oldstr.Replace(textBox1.Text, "");
                }
            }
            catch (Exception)
            {

                MessageBox.Show("没有符合条件的文件");
            }




        }

        private void 执行修改_Click(object sender, EventArgs e)
        {

            Computer computer = new Computer();
            try
            {

                for (int i = 0; i < index1; i++)
                {

                    computer.FileSystem.RenameFile(label1.Text + "\\" + (string)dataGridView1.Rows[i].Cells[1].Value, (string)dataGridView1.Rows[i].Cells[2].Value);

                }
                MessageBox.Show("操作成功");
                刷新文件();
            }
            catch (Exception)
            {

                MessageBox.Show("没有符合条件的文件");
            }


        }

        private void 替换文本_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < index1; i++)
                {
                    string oldstr = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    dataGridView1.Rows[i].Cells[2].Value = oldstr.Replace(textBox2.Text, textBox3.Text);
                }
            }
            catch (Exception)
            {

                MessageBox.Show("没有符合条件的文件");
            }
        }

        public void 选择文件夹()
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "选择文件夹"; ;//左上角提示
            string path = string.Empty;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                path = dialog.SelectedPath;//获取选中文件路径
            }
            label1.Text = path;
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] fi = di.GetFiles();


        }

        public void 刷新文件()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("序号");
            dt.Columns.Add("旧名称");
            dt.Columns.Add("新名称");
            DirectoryInfo di = new DirectoryInfo(label1.Text);
            FileInfo[] fi = di.GetFiles();
            index1 = fi.Length;
            for (int i = 0; i < fi.Length; i++)
            {
                dt.Rows.Add(i + 1, fi[i].Name);
            }


            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[1].ReadOnly = true;
            // 让 DataGridView1 的所有列宽自动调整一下。

            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

        private void 去除指定位置及长度字符_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < index1; i++)
                {
                    string oldstr = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    string substr = oldstr.Substring(int.Parse(textBox5.Text) - 1, int.Parse(textBox6.Text));
                    dataGridView1.Rows[i].Cells[2].Value = oldstr.Replace(substr, "");
                }
            }
            catch (Exception)
            {

                MessageBox.Show("没有符合条件的文件");
            }
        }

    }
}